package cn.iocoder.yudao.module.ym.job;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
import cn.iocoder.yudao.module.infra.dal.mysql.config.ConfigMapper;
import cn.iocoder.yudao.module.infra.enums.TicketStatusEnum;
import cn.iocoder.yudao.module.ym.dal.dataobject.site.SiteDO;
import cn.iocoder.yudao.module.ym.dal.dataobject.ticket.TicketDO;
import cn.iocoder.yudao.module.ym.dal.dataobject.tickettrajectory.TicketTrajectoryDO;
import cn.iocoder.yudao.module.ym.dal.mysql.site.SiteMapper;
import cn.iocoder.yudao.module.ym.dal.mysql.ticket.TicketMapper;
import cn.iocoder.yudao.module.ym.dal.mysql.tickettrajectory.TicketTrajectoryMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 定时删除工单数据
 *
 * @author j-sentinel
 */
@Slf4j
@Component
public class DeleteDataJob implements JobHandler {
    static final String YM_DELETED_DAY_KEY = "ym_delete_day";

    @Resource
    private TicketTrajectoryMapper ticketTrajectoryMapper;

    @Resource
    private ConfigMapper configMapper;

    @Override
    @TenantIgnore
    @Transactional
    public String execute(String param) {
        System.out.println("删除数据-------------------------------"+param);
        // 1. 获取删除数据超时时间
        ConfigDO configDO = configMapper.selectByKey(YM_DELETED_DAY_KEY);
        if(configDO == null){
            return "定时删除数据 0 条";
        }
        String value = configDO.getValue();
        long day = Long.parseLong(value);
        int delete = ticketTrajectoryMapper.delete(new LambdaQueryWrapperX<TicketTrajectoryDO>()
                .apply("TO_DAYS(NOW())-TO_DAYS(create_time)>" + day));
        return String.format("定时删除数据 %s 条",delete);
    }

}
